-- 2D 的直线运动
local jass = require 'jass.common'
local japi = require 'jass.japi'
local msg  = require 'jass.message'

local mover = { }
setmetatable(mover,mover)
ac.mover.class['line'] = mover

local mt = { }
mover.__index = mt 
extends(ac.mover)(mt)

-- 运动类型
mt.mover_type = 'line'

-- 起始点
mt.start = nil

-- 距离
mt.distance = 1000

-- 方向
mt.angle = 0.0

-- 速度
mt.speed = 1000

-- 加速度
mt.accel = 0

-- 设置方向
function mt:set_angle(angle)
    self.angle = angle
end 

-- 获取方向
function mt:get_angle()
    return self.angle 
end

-- 设置速度
function mt:set_speed(speed)
    self.speed = speed 
end 

-- 获取速度
function mt:get_speed()
    return self.speed
end

-- 运动
function mt:move()
    local point = self:get_point()
    local angle = self:get_angle() 
    local speed = self.frame * self:get_speed()
    local object = self.mover
    local distance = self.distance
    local moved_distance = self.moved_distance
    local target = point - { angle, speed}
    
    self.speed = self.speed + self.frame * self.accel
    if self:notify ('on_move',target) == false then 
        self:finish()
    else
        if self.turn_flag then 
            self:set_rotate(0, 0, angle)
        end
        self:set_point(target)
        self.moved_distance = self.moved_distance + speed
        if self.moved_distance >= self.distance then 
            self:finish()
        end 
    end 
end 

function mt:init()
    ac.mover.__index.init(self)
    self.moved_distance = 0
    if self.start then 
        if self.start.type=='point' then 
            self.start.z = self.start:get_height()
        end 
        self:set_point(self.start:get_point())
        self:set_height(self.start.z + (self.height or 0))
    end 
end 

return mover